<?php
namespace app\home\model;

use think\Db;
use think\Request;
use think\Model;
use think\Session;

class Details extends Model{

    //课程详情页首页数据获取
    public function index($id){

        $details = Db::table('courses')
                ->alias("co")
                ->join('users u','co.user_id = u.user_id')
                ->where('courses_id',$id)
                ->find();

        return $details;
    }

    //评论数据处理
    public function videOcom(){

        $getRequest = Request::instance()->post();

        $data = array();
        $data['comment_text'] = $getRequest['comment_text'];
        $data['courses_id'] = $getRequest['courses_id'];
        $data['user_id'] = $getRequest['user_id'];
        $data['comment_time'] = time();

        $res = Db::table("videocom")->insert($data);

       if($res == 1){
           $videocom = Db::table('videocom')
            ->alias("vid")
            ->where('courses_id',$data['courses_id'])
            ->join('users u','vid.user_id = u.user_id')
            ->limit(1)
            ->order('comment_time desc')
            ->select();

           return $videocom;
       }else{
           return 1;
       }
    }

    //收藏
    public function collection(){

        $getRequest = Request::instance()->post();

        $data = array();
        $data['courses_id'] = $getRequest['courses_id'];
        $data['collect_iscollect'] = 0;
        $data['currentUser_id'] = Session::get("USERS.user_id");
        $data['collect_time'] = time();

        $res = Db::table("collect")->insert($data);

        return $res;


    }


}